import java.util.HashMap;
import java.util.Map;

public class ContainsNearbyDuplicate {
    public static boolean code(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();

        for (int i = 0; i < nums.length; i++){
            if (hash.containsKey(nums[i])){
                int x = i - hash.get(nums[i]);
                if (x <= k){
                    return true;
                }

                hash.put(nums[i], i);
            }
        }

        return false;
    }

    public static void main(String[] args) {
        ContainsNearbyDuplicate.code(new int[]{1, 0, 1, 1}, 1);
    }
}